<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/11/2
 * Time: 20:03
 */
namespace app\models;

use think\Model;

class PackageModel extends Model
{
    protected $table = 'edu_package';

    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        $result = self::where($where)->order($order)->paginate($limit, false, ['page' => $page]);
        if (!$result->total()) {
            return ['code' => 0, 'msg' => "ok", 'count' => 0, 'data' => []];
        }
        foreach ($result as $key => $value) {
            $result[$key]['course_ascription'] = DictionaryModel::where('id',$value['course_ascription'])->value('name');
            $course_arr = explode(',',$result[$key]['course']);
            $course = '';
            foreach ($course_arr as $v){
                $course .= ','.CourseModel::where('id',$v)->value('course_name');
            }
            $arr = explode(',', $course);
            unset($arr[0]);
            $result[$key]['course'] = implode(',', $arr);
            $result[$key]['year'] = DictionaryModel::where('id',$value['year'])->value('name');//课程年份
            $result[$key]['grade'] = DictionaryModel::where('id',$value['grade'])->value('name');//学员年级
            $result[$key]['status_text'] = $value->status_text;
            $result[$key]['totalprice'] = Number_format($result[$key]['totalprice'] / 100,2);
            $result[$key]['discount'] = Number_format($result[$key]['discount'] / 100,2);
            $result[$key]['receivable'] = Number_format($result[$key]['receivable'] / 100,2);
        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }

    /**
     * 状态
     * @param $value
     * @param $data
     * @return mixed
     * 0：无效，1：有效
     */
    public function getStatusTextAttr($value, $data)
    {
        $status = [1 => "已启用", 0 => "已禁用"];
        return $status[$data['status']];
    }
}